//
//  BookCateVC.m
//  LiteraryTune
//
//  Created by 陈雪丹 on 2018/5/31.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "BookCateVC.h"
#import "BookListModel.h"
#import "BookCollectCell.h"
#import "BookDetailShowViewController.h"

@interface BookCateVC () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *showCollectionView;
//数据源
@property (nonatomic, strong)NSArray *dataArray;

@end

@implementation BookCateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getNetData];
    [self buildUI];
}

- (void)getNetData {
    __weak typeof(self) weakSelf = self;
    [CXDNetHelp getRequestWithURLString:[NSString stringWithFormat:@"%@%@",PartyPolicyURL,_cateId] parameters:nil success:^(id  _Nullable responseObject) {
        //        NSLog(@"%@",responseObject);
        weakSelf.dataArray = [NSArray yy_modelArrayWithClass:[BookListModel class] json:responseObject];
        [weakSelf.showCollectionView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)buildUI {
    [self.view addSubview:self.showCollectionView];
}
#pragma mark - 懒加载
- (UICollectionView *)showCollectionView {
    if (!_showCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreenWidth ) / 3, 150);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _showCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight ) collectionViewLayout:layout];
        _showCollectionView.delegate = self;
        _showCollectionView.dataSource = self;
        _showCollectionView.backgroundColor = RGB(238, 238, 238);
        _showCollectionView.showsVerticalScrollIndicator = NO;
        [_showCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([BookCollectCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class])];
    }
    return _showCollectionView;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookCollectCell class]) forIndexPath:indexPath];
    cell.backView.hidden = YES;
    BookListModel *tempModel = _dataArray[indexPath.item];
    [cell.bookImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RootHomeURL,tempModel.spic]]];
    cell.nameLabel.text = tempModel.news_tit;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (![CXDHelper getUserLoginStatus] && [NSUserDefaults.standardUserDefaults boolForKey:K_NeedLogin]) {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
        [CXDNoticeView showNoticeViewWithString:@"请先进行登陆"];
        return;
    }
    BookDetailShowViewController *detailVC = [[BookDetailShowViewController alloc] init];
    BookListModel *tempModel = _dataArray[indexPath.item];
    detailVC.navigationItem.title = tempModel.news_tit;
    detailVC.bookId = tempModel.bookID;
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end
